package P3排序算法;

public class BinarySearch {
    private static int count = 0;
    public static void main(String[] args) {
        int[] arr = {1, 5, 12, 21, 30, 32, 55, 68, 80, 89, 90, 99};
        int key = 99;
        int index = binarySearch(arr, 0, arr.length - 1, key);
        System.out.println(index);
        System.out.println("count = " + count);
    }

    private static int binarySearch(int[] arr, int low, int high, int key) {
        if (low > high) {
            return -1;
        }
        count++;
        int mid = (high - low) / 2 + low;
        if (arr[mid] == key) {
            return mid;
        } else if (key < arr[mid]) {
            return binarySearch(arr, low, mid - 1, key);
        } else {
            return binarySearch(arr, mid + 1, high, key);
        }
    }
}